﻿using AutoMapper;
using B.S.Procurement.Domain;
using B.S.Procurement.Error;
using B.S.Procurement.Write.Api.Application.Command.PurchaseDetailsCommand;
using MediatR;
using SqlSugar;

namespace B.S.Procurement.Write.Api.Application.CommandHander.PurchaseDetailsCommandHander
{
    public class UpdPurchaseDetailsCommandHander : IRequestHandler<UpdPurchaseDetailsCommand, ApiResult<int>>
    {
        private readonly IMapper mapper;
        private readonly ISqlSugarClient db;

        public UpdPurchaseDetailsCommandHander(IMapper mapper, ISqlSugarClient db)
        {
            this.mapper = mapper;
            this.db = db;
        }

        public async Task<ApiResult<int>> Handle(UpdPurchaseDetailsCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();

            var model = mapper.Map<PurchaseDetails>(request);

            model.Del = false;

            result.Data = await db.Updateable<PurchaseDetails>(model).ExecuteCommandAsync();

            if (result.Data > 0)
            {
                result.Code = ApiEnum.success;
                result.Msg = "修改采购明细成功";
            }
            else
            {
                result.Code = ApiEnum.error;
                result.Msg = "修改采购明细失败";
            }

            return result;
        }
    }
}
